//
//  HelpCenterViewController.m
//  Civacrm
//
//  Created by zhk on 2017/2/7.
//  Copyright © 2017年 王桂敏. All rights reserved.
//

#import "HelpCenterViewController.h"
#import "QuestionModel.h"
#import "HelpTableViewCell.h"

static NSString *HelpIdentifier = @"HelpCell";

@interface HelpCenterViewController ()

@property (nonatomic , strong) UITableView * HelpTableView;

@property (nonatomic,strong) NSMutableArray *dataSource;

@property (nonatomic,assign) NSInteger openRow;

@end

@implementation HelpCenterViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigation];
    [self initData];
    [self initView];
    [self initNetWork];
}
- (void)initNavigation
{
    self.title = @"帮助中心";
}
- (void)initData{
    self.openRow = -1;
    self.dataSource = [NSMutableArray array];
}

- (void)initView
{
    
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64);
    [self.tableView registerClass:[HelpTableViewCell class] forCellReuseIdentifier:HelpIdentifier];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = GlobalBgColor;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    QuestionModel *model = self.dataSource[indexPath.row];
    model.isOpen = !model.isOpen;
    self.openRow = indexPath.row;
    [self.tableView reloadData];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HelpTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HelpIdentifier forIndexPath:indexPath];
    if (indexPath.row != self.openRow) {
        QuestionModel *model = self.dataSource[indexPath.row];
        model.isOpen = NO;
    }
    cell.tableView = self.tableView;
    [cell configCellWithModel:self.dataSource[indexPath.row]];

    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [self.dataSource[indexPath.row] finalRowHeight];
}

- (void)handleMoreButtonAction:(UIButton *)button
                         event:(UIEvent *)event{
    NSSet *touchs = [event allTouches];
    UITouch *touch = [touchs anyObject];
    
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
    
    QuestionModel *model = self.dataSource[indexPath.row];
    model.isOpen = !model.isOpen;
    self.openRow = indexPath.row;
    [self.tableView reloadData];
    
    
}

- (void)tableView:(UITableView *)tableView
     touchesBegan:(NSSet *)touches
        withEvent:(UIEvent *)event{
    NSSet *touchs = [event allTouches];
    UITouch *touch = [touchs anyObject];
    
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
    if (!indexPath) {
        self.openRow = -1;
        [self.tableView reloadData];
    } else {
        [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
    }
}


- (void)initNetWork
{
    [self.manager postWithURL:NET_HelpCenter param:self.params delegate:self isEncrypt:NO type:NetWorkingType0];
}

- (void)networkingHandlefail:(NetworkingHandle *)handle type:(NetWorkingType)type
{
    [self showErrorWithStatus:@"请求失败"];
}

- (void)networkingHandleWarning:(NetworkingHandle *)handle responseObject:(id)responseObject type:(NetWorkingType)type
{
    [self showErrorWithStatus:responseObject[@"returnInfo"]];
}

- (void)networkingHandleSuccess:(NetworkingHandle *)handle parameters:(NSDictionary *)parameters responseObject:(id)responseObject type:(NetWorkingType)type
{
    
    if ([responseObject isKindOfClass:[NSArray class]] ) {
        NSArray * array = [NSArray array];
        array = responseObject;
        for (int i = 0; i < array.count; i ++) {
            QuestionModel * model = [[QuestionModel alloc]init];
            [model setValuesForKeysWithDictionary:array[i]];
            
//            model.title = [model.title stringByReplacingOccurrencesOfString:@"&ldquo;" withString:@"\""];
//            model.title = [model.title stringByReplacingOccurrencesOfString:@"&rdquo;" withString:@"\""];
//            
//            model.content = [model.content stringByReplacingOccurrencesOfString:@"&ldquo;" withString:@"\""];
//            model.content = [model.content stringByReplacingOccurrencesOfString:@"&rdquo;" withString:@"\""];
            
            
            [self.dataSource addObject:model];
        }
        [self.tableView reloadData];
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
